<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="bbs.*,core.*,util.*" %>
<%@ page import="java.util.*" %>
<!DOCTYPE html>
<html>
<head>
<%@ include file="/WEB-INF/include/header.jsp" %>
<style>
    div.header span:nth-child(1) { color: #aaa; }
    div.header span:nth-child(3) { color: #aaa; margin-left: 20px; }
    div.header span:nth-child(5) { margin-left: 20px; }
    h2 small { font-size: 10pt; }
    div.body { min-height: 400px; }
    iframe { width: 100%;  border: none; }
</style>
</head>
<%
    RequestParameter param = new RequestParameter(request);
    int bid = param.getInt("bid", 0);
    int aid = param.getInt("aid", 0);

    Article article = ArticleDAO.selectById(aid);
    BoardService boardService = new BoardService(request, bid);
    if (boardService.canReadArticle(article))
        new UnauthorizedAccessException("게시글 보기 권한 없음");

    int userId = boardService.getCurrentUserId();
    if (userId > 0) ArticleDAO.insertReadLog(aid, userId);

    String urlList = "articleList.jsp?" + request.getQueryString().replaceAll("&?aid=[0-9]+&?","");
    String urlEdit = "articleEdit.jsp?" + request.getQueryString();
    String urlDelete = "articleAction.jsp?cmd=delete&" + request.getQueryString();
    String urlNotice = "articleAction.jsp?cmd=notice&" + request.getQueryString();
    String urlReplay = "articleCreate.jsp?bid=" + bid + "&aid=" + aid;
%>
<body>
<%@ include file="/WEB-INF/include/menu.jsp" %>

<div class="container">

    <div class="controls pull-right">
        <a href="<%= urlList %>" class="btn btn-small">목록으로</a>
        <% if (boardService.canManageBoard()) { %>
            <a class="btn btn-small" href="<%= urlNotice %>">
                공지글 <%= article.isNotice() ? "취소" : "설정" %></a>
        <% } %>
        <% if (boardService.canEditArticle(article)) { %>
            <a class="btn btn-small" href="<%= urlEdit %>">수정</a>
        <% } %>
        <% if (boardService.canDeleteArticle(article)) { %>
            <a class="btn btn-small" href="<%= urlDelete %>"
                onclick="return confirm('삭제하시겠습니까?');">삭제</a>
        <% } %>
        <% if (boardService.canCreateArticle()) { %>
            <a class="btn btn-small" href="<%= urlReplay %>">댓글쓰기</a>
        <% } %>
    </div>

    <h1><%= boardService.getBoard().getTitle() %></h1>
    <hr />

    <h3><small>[<%= article.getCategory() %>]</small> <%= article.getTitle() %></h3>
    <hr />

    <div class="header">
        <span>작성자:</span>
        <span><%= article.getName() %></span>
        <span>작성일:</span>
        <span><%= DateUtil.toYMDHMS(article.getWriteTime()) %></span>
        <% if (article.isNotice()) { %>
            <span class="label label-important">공지</span>
        <% } %>
    </div>
    <hr />

     <div class="body">
        <%= article.getBody() %>
    </div>

    <% if (boardService.getBoard().isFileAttachEnabled()) { %>
        <iframe src="fileDownloadList.jsp?bid=<%= bid %>&aid=<%= aid %>">
        </iframe>
    <% } %>

    <% if (boardService.getBoard().isCommentEnabled()) { %>
        <hr />
        <iframe src="commentList.jsp?bid=<%= bid %>&aid=<%= aid %>">
        </iframe>
    <% } %>

</div>

<%@ include file="/WEB-INF/include/footer.jsp" %>
</body>
</html>
